package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharRespawnList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GDCharRespawnPane extends AdjustablePanel {
    public static final int SEL_SPAWN_NONE = 0;
    public static final int SEL_SPAWN_DEVILS_CROSSING = 1;
    public static final int SEL_SPAWN_HOMESTAEAD = 2;
    public static final int SEL_SPAWN_FORT_IKON = 3;
    private static String[] spawnPoints;
    static {
        GDCharRespawnPane.spawnPoints = null;
        (GDCharRespawnPane.spawnPoints = new String[4])[0] = "";
        GDCharRespawnPane.spawnPoints[1] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SPAWN_DEVILS_CROSSING");
        GDCharRespawnPane.spawnPoints[2] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SPAWN_HOMESTEAD");
        GDCharRespawnPane.spawnPoints[3] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SPAWN_FORT_IKON");
    }
    private int difficulty;
    private boolean changed;

    public GDCharRespawnPane(final int difficulty) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbRespawn));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbRespawn));
        layout.setVerticalGroup(vGroup);
    }

    private JComboBox cbRespawn;

    public boolean hasChanged() {
        return this.changed;
    }

    public GDCharUID getSpawnPoint() {
        final int idx = this.cbRespawn.getSelectedIndex();
        GDCharUID spawn = null;
        switch (idx) {
            case 1: {
                spawn = GDCharRespawnList.UID_SPAWN_DEVILS_CROSSING;
                break;
            }
            case 2: {
                spawn = GDCharRespawnList.UID_SPAWN_HOMESTEAD;
                break;
            }
            case 3: {
                spawn = GDCharRespawnList.UID_SPAWN_FORT_IKON;
                break;
            }
        }
        return spawn;
    }

    public void setChar(final GDChar gdc) {
        this.changed = false;
        GDCharUID uid = null;
        if (gdc != null) {
            uid = gdc.getRespawnPoint(this.difficulty);
        }
        if (uid == null) {
            this.cbRespawn.setSelectedIndex(0);
        } else {
            if (uid.equals(GDCharRespawnList.UID_SPAWN_DEVILS_CROSSING)) {
                this.cbRespawn.setSelectedIndex(1);
            }
            if (uid.equals(GDCharRespawnList.UID_SPAWN_HOMESTEAD)) {
                this.cbRespawn.setSelectedIndex(2);
            }
            if (uid.equals(GDCharRespawnList.UID_SPAWN_FORT_IKON)) {
                this.cbRespawn.setSelectedIndex(3);
            }
        }
    }

    public void updateChar(final GDChar gdc) {
        if (gdc == null) {
            return;
        }
        if (!this.changed) {
            return;
        }
        final GDCharUID spawn = this.getSpawnPoint();
        if (spawn != null) {
            gdc.setRespawnPoint(this.difficulty, spawn);
        }
        this.changed = false;
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SPAWN_POINT"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbRespawn == null) {
            this.cbRespawn = new JComboBox(GDCharRespawnPane.spawnPoints);
            this.cbRespawn.addActionListener(new ChangeActionListener());
        }
        this.cbRespawn.setFont(fntCombo);
        this.cbRespawn.setMaximumSize(new Dimension(1000, 3 * GDStashFrame.iniConfig.sectUI.fontSize));
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharRespawnPane.this.changed = true;
        }
    }
}
